home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 0574.ZIP / RNEWS.C < prev    next >
Text File  |  1986-08-12  |  15KB  |  611 lines

  1. /*
  2.     $Source: BOB/USENET/rnews.c%v $
  3.  
  4.        This file is part of the UN*X Gateway Fido/UNIX code.
  5.        This implements reading UNIX messages and placing them
  6.        into the proper message bases.  Files, USENET messages,
  7.        and mail messages are all handled here.
  8. */
  9.  
  10. /*
  11.  * $Log:    rnews.c $
  12.  * Revision 1.1  85/12/10  01:46:22  rch
  13.  * Initial revision
  14.  * 
  15.  */
  16.  
  17. #include <dos.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <stat.h>
  21.  
  22. #define MAX_SIZE 9000L
  23. #define TEMPNAME "JUNKXXXX.XXX"
  24. #define MSGPRIVATE    1        /* private message */
  25. #define MSGBROAD    2        /* broadcast message */
  26. #define MSGREAD        4        /* read by addressee */
  27. #define MSGSENT        8        /* sent OK (remote) */
  28. #define MSGFILE        16        /* file attached to msg */
  29. #define MSGFWD        32        /* being/was forwarded */
  30. #define MSGORPHAN    64        /* unknown dest node */
  31. #define MSGKILL        128        /* Kill when sent */
  32. char *template = TEMPNAME;
  33.     static struct {
  34.         char rsvd[21];
  35.         char attr;
  36.         long ftime;
  37.         long fsize;
  38.         char fname[13];
  39.     } sbuf, sbuf1;
  40. struct _msg {
  41.     char from[36];
  42.     char to[36];
  43.     char subj[72];
  44.     char date[20];
  45.     int times;
  46.     int dest;
  47.     int orig;
  48.     int cost;
  49.     int orig_net;
  50.     int dest_net;
  51.     int caca[4];
  52.     unsigned reply;
  53.     int attr;
  54.     int up;
  55. } mess_head;
  56.  
  57.  
  58. main (argc, argv)
  59. int argc;
  60. char *argv[];
  61. {
  62.     extern char *mktemp(), *fgets ();
  63.     char line[BUFSIZ], *tmpfile, t1[BUFSIZ];
  64.     int from_file;
  65.     char datestamp[20];
  66.     char newsgroups[BUFSIZ];
  67.     char bang[3], rnews[128];
  68.     char filename[80], *underscore, *t;
  69.     int rm = 1;
  70.     int j, k, a, q, got_blank;
  71.     long size, i;
  72.     extern char *index ();
  73.     FILE *input, *output;
  74.  
  75.     date ("%d %n %y  ", datestamp);
  76.     time ("%h:%m:%s", &datestamp[11]);
  77.     strcpy (mess_head.date, datestamp);
  78.     mess_head.times = 0;
  79.     mess_head.orig = 101;
  80.     mess_head.cost = 0;
  81.     mess_head.orig_net = 132;
  82.     mess_head.reply = 0;
  83.     mess_head.up = 0;
  84.  
  85.     printf ("\n\n***************** %s ********************\n\n", datestamp);
  86.  
  87.     k = 1;
  88. /*    while (--argc) */
  89.        {
  90.          a = 0;
  91.        j = 1;
  92.        filedir (argv[k], a++, filename);
  93.        if (filename[0] == '\0')
  94.           printf ("******* No files match %s\n", argv[k]);
  95. /*       while (filename[0] != '\0') */
  96.           {
  97.          if ((input = fopen (filename, "r")) == NULL)
  98.                 {
  99.                 printf ( "******* Not able to open %s\n", filename);
  100. /*                filedir (argv[k], 0, filename);
  101.                 for (q = 1; q < a; q++)
  102.                    filedir (argv[k], q, filename);
  103.                 filedir (argv[k], a++, filename);
  104.                 continue; */
  105.                 exit (1);
  106.                 }
  107.           printf ("******* Processing %s ********\n", filename);
  108.           from_file = 1;
  109.           while (!feof(input))
  110.                 {
  111.                 if (from_file)
  112.                    {
  113.                    if (fscanf (input, "%s %s %ld", bang, rnews, &size) == EOF)
  114.                       break;
  115.                      if (getc (input) == EOF)
  116.                       break;
  117.                    }
  118.                 else
  119.                    if (sscanf (line, "%s %s %ld", bang, rnews, &size) == EOF)
  120.                        break;
  121.  
  122.                 printf ("Processing article number %d\n", j++);
  123.                  strcpy (template, TEMPNAME);
  124.                  tmpfile = mktemp (template);
  125.                  output = fopen (tmpfile, "w");
  126.                  mess_head.subj[0] = '\0';
  127.  
  128.                  got_blank = 0;
  129.                  for (i = 0L; i < size; i += (long) strlen (line))
  130.                     {
  131.                     from_file = 1;
  132.                     if (fgets (line, BUFSIZ, input) == NULL)
  133.                         break;
  134.                     if (strncmp (line, "Newsgroups: ", 12) == 0)
  135.                         strcpy (newsgroups, &line[12]);
  136.                     if (strncmp (line, "Subject: ", 9) == 0)
  137.                         strncpy (mess_head.subj, &line[9], 70);
  138.                     if (strncmp (line, "#! ", 3) == 0)
  139.                        {
  140.                        from_file = 0;
  141.                        break;
  142.                        }
  143.                     if (!got_blank)
  144.                        if (sscanf (line, "%s:", t1) != 1)
  145.                           got_blank = 1;
  146.                        else
  147.                           if (strncmp (line, "Path: ", 6) == 0)
  148.                                  fprintf (output, "%s", line);
  149.                           else if (strncmp (line, "From: ", 6) == 0)
  150.                                  fprintf (output, "%s", line);
  151.                           else if (strncmp (line, "Date: ", 6) == 0)
  152.                                  fprintf (output, "%s", line);
  153.                           else
  154.                                  line[0] = '\0';
  155.                     if (got_blank)
  156.                    fprintf (output, "%s", line);
  157.                     }
  158.                  fclose (output);
  159.                  if (strncmp (rnews, "FILE", 4) == 0)
  160.                     {
  161.                     printf ("   Attempting to create %s\n", &rnews[5]);
  162.                     filecopy (tmpfile, &rnews[5], 1);
  163.                     unlink (tmpfile);
  164.                     }
  165.                  else
  166.                      if (strncmp (rnews, "MAIL", 4) != 0)
  167.                         if (size > MAX_SIZE)
  168.                             {
  169.                             printf ("Message too large:  %ld bytes is larger than max %ld\n",
  170.                                 size, MAX_SIZE);
  171.                             unlink (tmpfile);
  172.                             }
  173.                         else if (strncmp (mess_head.subj, "cancel ", 7) == 0)
  174.                            {
  175.                            printf ("message was a cancel message\n");
  176.                            unlink (tmpfile);
  177.                            }
  178.                         else
  179.                            {
  180.                          strcpy (mess_head.from, "Usenet");
  181.                        strcpy (mess_head.to, "All");
  182.                         mess_head.dest = 101;
  183.                             mess_head.attr = 256;
  184.                             mess_head.dest_net = 132;
  185.                             change_name (tmpfile, newsgroups);
  186.                            }
  187.                      else
  188.                         {
  189.                     strcpy (mess_head.from, "Usenet");
  190.                         if (sscanf (&rnews[5], "%d!%d!%s", &mess_head.dest_net,
  191.                             &mess_head.dest, mess_head.to) != 3)
  192.                              sscanf (&rnews[5], "%d/%d!%s", &mess_head.dest_net,
  193.                                 &mess_head.dest, mess_head.to);                   
  194.                         underscore = index (mess_head.to, '_');
  195.                         mess_head.attr = MSGKILL + MSGPRIVATE + 256;
  196.                         while (underscore != NULL)
  197.                            {
  198.                            *underscore = ' ';
  199.                            ++underscore;
  200.                            underscore = index (underscore, '_');
  201.                            }
  202.                
  203.                         t = mess_head.to;
  204.                         *t = toupper (t[0]);
  205.                         (void) strlower (&t[1]);
  206.                         while ((t = index (t, ' ')) != NULL)
  207.                            {
  208.                            ++t;
  209.                            *t = toupper (t[0]);
  210.                            }
  211.  
  212.                         domail (tmpfile);
  213.                         }
  214.                  }
  215.           fclose (input);
  216.             if (rm)
  217.                  unlink (filename);
  218. /*         filedir (argv[k], 0, filename);
  219.           for (q = 1; q < a; q++)
  220.               filedir (argv[k], q, filename);
  221.           filedir (argv[k], a++, filename); */
  222.           }
  223.        ++k;
  224.       }
  225.     }
  226.  
  227. changeit (groupname, oldchar, newchar)
  228. char *groupname, oldchar, newchar;
  229. {
  230.     char *tmp;
  231.  
  232.     tmp = groupname;
  233.     while (*tmp != '\0')
  234.         {
  235.         if (*tmp == oldchar)
  236.             *tmp = newchar;
  237.         ++tmp;
  238.         }
  239. }
  240.  
  241. addnumber (newname)
  242. char *newname;
  243. {
  244.     int i, max;
  245.     char tmp1[100], filename[100];
  246.  
  247.     max = 0;
  248.       strcpy (tmp1, newname);
  249.       strcat (tmp1, "/*.MSG");
  250.       filedir (tmp1, 0, filename);
  251.       while (filename[0] != '\0')
  252.      {
  253.      sscanf (filename, "%d.", &i);
  254.      if (i > max)
  255.         max = i;
  256.      filedir (tmp1, 1, filename);
  257.      }
  258.       ++max;
  259.    sprintf (newname, "%s/%d.MSG", newname, max);
  260. }
  261.  
  262. filecopy (oldname, newname, n)
  263. char *oldname, *newname;
  264. int n;
  265. {
  266.     int in, out, c;
  267.     char buf[BUFSIZ];
  268.  
  269.     in = open (oldname, O_RDONLY);
  270.     out = open (newname, O_CREAT+O_WRONLY);
  271.     if (!n)
  272.     write (out, &mess_head, sizeof (struct _msg));
  273.     while ((c = read (in, buf, BUFSIZ)) == BUFSIZ)
  274.         write (out, buf, BUFSIZ);
  275.     if (c != 0)
  276.         write (out, buf, c);
  277.     close (in);
  278.     close (out);
  279. }
  280.  
  281. change_name (oldname, groupname)
  282. char *oldname, *groupname;
  283. {
  284.     char newname[100];
  285.     extern char *index ();
  286.     char *comma;
  287.  
  288.     changeit (groupname, '.', '/');
  289.     comma = groupname;
  290.     while (comma != NULL)
  291.         {
  292.         strcpy (newname, "C:/FIDO/USENET/");
  293.         comma = index (groupname, ',');
  294.         if (comma != NULL)
  295.             *comma = '\0';
  296.         else
  297.             groupname[strlen(groupname)-1] = '\0';
  298.         strcat (newname, groupname);
  299.         addnumber (newname);
  300.         if (newname[0] != '\0')
  301.             {
  302.             printf ("  It is becoming '%s'\n", newname);
  303.             filecopy (oldname, newname, 0);
  304.             }
  305.         if (comma != NULL)
  306.             groupname = comma + 1;
  307.         }
  308.     unlink (oldname);
  309. }        
  310.  
  311. domail (filename)
  312. char *filename;
  313. {
  314.     char newname[100];
  315.     extern char *index ();
  316.     char *comma;
  317.  
  318.     strcpy (newname, "C:/FIDO/FIDOMAIL");
  319.     addnumber (newname);
  320.     if (newname[0] != '\0')
  321.         {
  322.         printf ("  It is mail that will become '%s'\n", newname);
  323.         filecopy (filename, newname, 0);
  324.         }
  325.     unlink (filename);
  326. }        
  327.  
  328. /*
  329.         date.c
  330.  
  331.         Author: Bob Hartman
  332.         Date:   6-5-85
  333.  
  334.         This file implements a standardized date routine which is
  335.         callable from Manx AZTEC C-86 compiler.  The format string
  336.         determines the way the date is to be formatted as follows:
  337.  
  338.                 %n - 3 character month name
  339.                 %N - Full month name
  340.                 %m - Month as 2 digits (zero filled if necessary)
  341.                 %d - Day as 2 digits (zero filled if necessary)
  342.                 %y - Year as 2 digits
  343.                 %Y - Year as 4 digits
  344.  
  345.                 A % not followed by one of the above is ignored.
  346.                 All other characters in the format specifier are
  347.                 copied to the output string.
  348. */
  349.  
  350. /* Character used to delimit format specifiers in format strings */
  351. #define ESCAPE_CHAR     '%'
  352. #define DOSINT   0x21
  353.  
  354. /* Month names for date() function */
  355. static char *_months[12] = {
  356.         "January", "February", "March",     "April",   "May",      "June",
  357.         "July",    "August",   "September", "October", "November", "December"
  358. };
  359.  
  360. /* Day of week names for date() function */
  361. static char *_days[7] = {
  362.     "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  363. };
  364.  
  365.  
  366. /*
  367.         date (fmt, d)
  368.         char *fmt;
  369.         char *d;
  370.  
  371.         returns in d, the current date in the form specified by the fmt string
  372. */
  373.  
  374. date (fmt, d)
  375. register char *fmt;
  376. register char *d;
  377. {
  378.     union REGS    inregs, outregs;
  379.     char    temp[5];        /* To hold date so it can be processed again */
  380.     int w, y, m, d1; 
  381.         
  382.     inregs.x.ax = 0x2a00;
  383.     (void) sysint (DOSINT, &inregs, &outregs);
  384.     w = outregs.h.al;
  385.     y = outregs.x.cx;
  386.     m = outregs.h.dh;
  387.     d1 = outregs.h.dl;
  388.     
  389.     /* Now go through the format string */
  390.     while (*fmt != '\0')
  391.         {
  392.         if (*fmt != ESCAPE_CHAR)
  393.             *d++ = *fmt;
  394.         else
  395.             {
  396.             switch (*(++fmt))
  397.                 {
  398.                 case 'n':
  399.                     sprintf (d, "%.3s", _months[m - 1]);
  400.                     d += 3;
  401.                     break;
  402.                 case 'N':
  403.                     sprintf (d, "%s", _months[m - 1]);
  404.                     d += strlen (d);
  405.                     break;
  406.                 case 'm':
  407.                     sprintf (d, "%02d", m);
  408.                     d += 2;
  409.                     break;
  410.                 case 'd':
  411.                     sprintf (d, "%02d", d1);
  412.                     d += 2;
  413.                     break;
  414.                 case 'w':
  415.                     sprintf (d, "%.3s", _days[w]);
  416.                     d += 3;
  417.                     break;
  418.                 case 'W':
  419.                     sprintf (d, "%s", _days[w]);
  420.                     d += strlen (d);
  421.                     break;
  422.                 case 'y':
  423.                     sprintf (temp, "%04d", y);
  424.                     sprintf (d, "%.2s", temp+2);
  425.                     d += 2;
  426.                     break;
  427.                 case 'Y':
  428.                     sprintf (d, "%04d", y);
  429.                     d += 4;
  430.                     break;
  431.                 default:
  432.                     *d++ = *fmt;
  433.                 }
  434.             }
  435.         ++fmt;
  436.         }
  437.     *d = '\0';      /* Just to be sure that it is null terminated */
  438. }
  439.  
  440.  
  441. /*
  442.         time (fmt, t)
  443.         char *fmt;
  444.         char *t;
  445.  
  446.         returns in t, the current time in the form specified by the fmt string
  447. */
  448.  
  449. time (fmt, t)
  450. register char *fmt;
  451. register char *t;
  452. {
  453.     union REGS    inregs, outregs;
  454.     int h, m, s, ms;
  455.  
  456.     inregs.x.ax = 0x2c00;
  457.     (void) sysint (DOSINT, &inregs, &outregs);
  458.     h = outregs.h.ch;
  459.     m = outregs.h.cl;
  460.     s = outregs.h.dh;
  461.     ms = outregs.h.dl;
  462.     /* Now go through the format string */
  463.     while (*fmt != '\0')
  464.         {
  465.         if (*fmt != ESCAPE_CHAR)
  466.             *t++ = *fmt;
  467.         else
  468.             {
  469.             switch (*(++fmt))
  470.                 {
  471.                 /* hours (24 hour clock) */
  472.                 case 'h':
  473.                     sprintf (t, "%02d", h);
  474.                     t += 2;
  475.                     break;
  476.                 /* hours (12 hour clock) */
  477.                 case 'H':
  478.                     sprintf (t, "%02d", (h % 12) ?    (h % 12) : 12);
  479.                     t += 2;
  480.                     break;
  481.                 /* minutes */
  482.                 case 'm':
  483.                     sprintf (t, "%02d", m);
  484.                     t += 2;
  485.                     break;
  486.                 /* seconds */
  487.                 case 's':
  488.                     sprintf (t, "%02d", s);
  489.                     t += 2;
  490.                     break;
  491.                 /* hundredths of a second */
  492.                 case 't':
  493.                     sprintf (t, "%02d", ms);
  494.                     t += 2;
  495.                     break;
  496.                 /* am/pm indicator */
  497.                 case 'i':
  498.                     sprintf (t, "%s", (h < 12) ? "am" : "pm");
  499.                     t += 2;
  500.                     break;
  501.                 /* AM/PM indicator */
  502.                 case 'I':
  503.                     sprintf (t, "%s", (h < 12) ? "AM" : "PM");
  504.                     t += 2;
  505.                     break;
  506.                 default:
  507.                     *t++ = *fmt;
  508.                 }
  509.             }
  510.         ++fmt;
  511.         }
  512.     *t = '\0';      /* Just to be sure that it is null terminated */
  513. }
  514.  
  515. filedir (name, times, ret_str)
  516. char *name;
  517. int times;
  518. char *ret_str;
  519. {
  520.  
  521.     char *ret_val;
  522.  
  523.     bdos (0x1a, &sbuf);
  524.     if (times == 0) {
  525.         if (dos (0x4e, 0, ~ST_VLABEL, name, 0, 0) == -1) 
  526.             sbuf.fname[0] = '\0';
  527.         strcpy (ret_str, sbuf.fname);
  528.     }
  529.     else {
  530.         if (dos (0x4f, 0, ~ST_VLABEL, name, 0, 0) == -1) 
  531.             sbuf.fname[0] = '\0';
  532.         strcpy (ret_str, sbuf.fname);
  533.     }
  534. }
  535.  
  536. strtrans (source, table)
  537. register char *source, *table;
  538. {
  539.     char *start_pointer;
  540.  
  541.                     /* Save the original source pointer
  542.                        so that it can be returned */
  543.     start_pointer = source;
  544.  
  545.                     /* Step through the source string */
  546.     do {
  547.  
  548.                     /* Do the translation */
  549.         *source = table[*source];
  550.  
  551.     } while (*++source);
  552.  
  553.                     /* Return the pointer to the result */
  554.     return (start_pointer);
  555.  
  556. }
  557.  
  558.                     /* This table contains character
  559.                        translation codes for
  560.                        converting upper case to
  561.                        lower case */
  562. static char low_table[256] = {
  563.       0,   1,   2,     3,   4,   5,    6,   7,   8,   9,
  564.      10,  11,  12,    13,  14,  15,  16,  17,  18,  19,
  565.      20,  21,  22,    23,  24,  25,  26,  27,  28,  29,
  566.      30,  31,  32,    33,  34,  35,  36,  37,  38,  39,
  567.      40,  41,  42,    43,  44,  45,  46,  47,  48,  49,
  568.      50,  51,  52,    53,  54,  55,  56,  57,  58,  59,
  569.      60,  61,  62,    63,  64,  97,  98,  99, 100, 101,
  570.     102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  571.     112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
  572.     122,  91,  92,    93,  94,  95,  96,  97,  98,  99,
  573.     100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  574.     110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  575.     120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
  576.     130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
  577.     140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
  578.     150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  579.     160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  580.     170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  581.     180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
  582.     190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
  583.     200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
  584.     210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  585.     220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
  586.     230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  587.     240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
  588.     250, 251, 252, 253, 254, 255
  589. };
  590.  
  591. /*
  592.  *    char *
  593.  *    strlower (str)
  594.  *    char *str;
  595.  *
  596.  *    This function returns the string str with all upper case characters
  597.  *    converted to lower case.  It simply calls strtrans (in module
  598.  *    strtrans.obj) with the above tranlation table.
  599.  */
  600.  
  601. strlower (str)
  602. register char *str;
  603. {
  604.  
  605.     return (strtrans (str, low_table));
  606.  
  607.  
  608.  
  609.  
  610.